package com.example.mystore.controller;

import com.example.mystore.model.ProductReview;
import com.example.mystore.model.User;
import com.example.mystore.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/reviews")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    // 获取商品评论列表
    @GetMapping("/product/{productId}")
    public List<ProductReview> getProductReviews(@PathVariable Long productId) {
        return reviewService.getProductReviews(productId);
    }

    // 提交评论
    @PostMapping
    public ResponseEntity<?> submitReview(@RequestBody ProductReview review) {
        User currentUser = getCurrentUser();
        reviewService.submitReview(currentUser.getId(), review);
        return ResponseEntity.ok().body("评论已提交");
    }

    // 点赞评论
    @PostMapping("/like/{reviewId}")
    public ResponseEntity<?> likeReview(@PathVariable Long reviewId) {
        User currentUser = getCurrentUser();
        reviewService.likeReview(currentUser.getId(), reviewId);
        return ResponseEntity.ok().body("已点赞");
    }

    // 获取当前登录用户（简化版）
    private User getCurrentUser() {
        // 实际项目中应从请求头中的令牌获取用户信息
        return new User(1L, "testuser", "password", "test@example.com");
    }
}
